from django.test import TestCase
from django.core import mail
from mappapp.models import Query
from django.utils.datetime_safe import date
import datetime
from django.conf import settings
from mappapp.tasks import run_mapp
from mappapp.settings import MAPP_BLAST_FILE
import os

class ViewsTests(TestCase):
    def setUp(self):
        pass
    
    def test_sending_email(self):
        """ test sending email """
        mail.send_mail('Subject here', 'Here is the message.',
            'from@example.com', ['onpelikan@gmail.com'],
            fail_silently=False)
        self.assertEquals(len(mail.outbox), 1)
        self.assertEquals(mail.outbox[0].subject, 'Subject here')



class TasksTests(TestCase):
    
    seqid = '1'
    sequence = 'MKPVV'
    mutations = 'M1V K2P'
    
    def setUp(self):
        with open(MAPP_BLAST_FILE.format(id=TasksTests.seqid), 'w') as f:
            f.write('>a\nMKPVB\n')
            f.write('>b\nVKPVV\n')
    
    def tearDown(self):
        os.remove(MAPP_BLAST_FILE.format(id=TasksTests.seqid))

    def test_run_mapp(self):
        run_mapp(TasksTests.seqid,
                 TasksTests.sequence,
                 TasksTests.mutations,
                 mail='onpel@seznam.cz',
                 new_settings={'blast' : 'echo mock blastprogram',}
                 )
    
        


